package org.example.ch07.sercive;

import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.example.ch07.entity.City;
import org.example.ch07.mapper.CityMapper;
import org.example.ch07.util.MybatisUtils;

import java.util.List;

/**
 * ClassName: CityService
 * Description:
 *
 * @Author zjl
 * @Create 2025/04/25 08:51
 * @Version 1.0
 */
public class CityService {
    public List<City> pageCity(int pageNum, int pageSize) {
        // 将分页参数封装成RowBounds对象
        RowBounds rowBounds = new RowBounds(pageNum, pageSize);
        try(SqlSession sqlSession = MybatisUtils.getSqlSession()) {
            return sqlSession.getMapper(CityMapper.class).pageCity(rowBounds);
        }
    }

    public PageInfo<City> pageCity2(int pageNum, int pageSize) {
        try (SqlSession sqlSession = MybatisUtils.getSqlSession()){
            List<City> list = sqlSession.getMapper(CityMapper.class).pageCity2(pageNum, pageSize);

            // 将分页记录封装成分页对象（总记录数、总分页数、起始页、下一页...）
            return new PageInfo<>(list);
        }
    }
}
